(function() {
  app.controller('vipIndexCtrl', function(header_m, $scope, local_m, vipIndex, $state, bottomdilog, tradeApi) {
    $scope.com_list = vipIndex.list; // 列表项数据

    $scope.init = function() {
      tradeApi.query({
        funcId: '300034',
        data: {}
      }).then(function(res) {
        angular.forEach(res, function(item, index) {
          item.txt = item.contract_id + (item.contract_status === '1' ? '（已解约）' : '');
          item.value = item.contract_id;
        }, this);
        $scope.select_options = res;
        $scope.change_vip(res[(sessionStorage.getItem('vip_account_index') || 1) - 1].contract_id);
      });
    };

    $scope.com_list_click = function(obj, index) { // 列表点击函数
      if (obj.go) { $state.go(obj.go, { contract_id: obj.contract_id }); return; }
      if (obj.txt === '更多账户') {
        bottomdilog
          .open({
            type: '2',
            title: '请选择账户',
            list: $scope.select_options,
            list_click: function(it, index) {
              sessionStorage.setItem('vip_account_index', index);
              $scope.change_vip(it.value);
            }
          });
      }
    };

    $scope.change_vip = function(id) {
      vipIndex.list[0].contract_id = id;
      vipIndex.list[5].contract_id = id;
      header_m.set({
        title_c: id,
      });
      tradeApi.query({
        funcId: '300037',
        data: {
          contract_id: id
        }
      }).then(function(res) {
        sessionStorage.setItem('eam_session', res.eam_session);
        $scope.get_vip_info();
      });
    };

    $scope.get_vip_info = function() {
      tradeApi
        .etQuery({
          funcid: '5',
        })
        .then(function(res) {
          $scope.vip_info = res[0];
        });
    };
  });

  app.filter('vipIndex_asset', function() {
    return function(input) {
      if (input === 0 || input === '0' || input === '0.00' || input === 0.00) {
        return input;
      }
      if (!input) {
        return '--';
      }
      return input;
    };
  });

  app.value('vipIndex', {
    list: [{
        txt: '追加保证金',
        jiantou: true,
        url: 'nav_icon3_click@3x.png',
        go: 'vip-deposit',
        contract_id: null,
      },
      {
        txt: '我的持仓',
        jiantou: true,
        url: 'pay-th-ion.png',
        go: 'vip-hold'

      },
      {
        txt: '委托查询',
        jiantou: true,
        url: 'apply-result-ion.png',
        go: 'vip-entrust-query'

      },
      {
        txt: '成交查询',
        jiantou: true,
        url: 'my-deal-ion.png',
        go: 'vip-deal-query'

      },
      {
        txt: '资金流水',
        jiantou: true,
        url: 'nav_icon1_click@3x.png',
        go: 'vip-payflow'
      },
      {
        txt: '提取盈利',
        jiantou: true,
        url: 'bank-card-ion.png',
        go: 'vip-profit',
        contract_id: null,
      },
      {
        txt: '更多账户',
        jiantou: true,
        url: 'nav_icon5_click@3x.png'
      },
    ]
  });
})();